/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipo.mm.ejb.facade.representative;

import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.ipo.mm.ejb.entities.Institution;
import org.ipo.mm.ejb.entities.Representative;
import org.ipo.mm.ejb.facade.institution.InstitutionFacadeLocal;
import org.ipo.mm.ejb.facade.log.LogFacadeLocal;
import org.ipo.mm.ejb.facade.meeting.MeetingFacadeLocal;
import org.ipo.mm.ejb.util.RepresentativeSearchQueryBuilder;

/**
 *
 * @author makcro
 */
@Stateless
public class RepresentativeFacade implements RepresentativeFacadeLocal, RepresentativeFacadeRemote {
    @EJB
    private LogFacadeLocal logFacade;

    @EJB
    private InstitutionFacadeLocal institutionFacade;
    @EJB
    private MeetingFacadeLocal meetingFacade;
    @PersistenceContext
    private EntityManager em;

    public void create(Representative representative) {
        em.persist(representative);
    }

    public void edit(Representative representative) {
        em.merge(representative);
    }

    public void remove(Representative representative) {
        em.remove(em.merge(representative));
    }

    public Representative find(Object id) {
        return em.find(Representative.class, id);
    }

    public List<Representative> findAll() {
        return em.createNamedQuery("Representative.findAll").getResultList();
    }



    public List<Representative> findWithEmail(){
             return em.createNamedQuery("Representative.findWithEmail").getResultList();

    }

    public void markAsDeleted(int primaryKey) {
        Representative representative = find(primaryKey);

//        Collection<Meeting> meetings = representative.getMeetingCollection();
//        for (Meeting meeting : meetings) {
//            meeting.setDeleted(true);
//            meetingFacade.edit(meeting);
//
//        }

        representative.setDeleted(true);
        edit(representative);

    }

    public void update(int id,
            String firstName,
            String lastName,
            int instituton,
            String telephone,
            String email,
            String fax,
            String function,
            String note) {

        Representative r = find(id);
        Institution i = institutionFacade.find(instituton);

        r.setEmail(email);
        r.setFax(fax);
        r.setFirstName(firstName);
        r.setFunction(function);
        r.setLastName(lastName);
        r.setNote(note);
        r.setTelephone(telephone);
        r.setInstitution(i);
        edit(r);



    }

    public List<Representative> search(String query) {

        Query q = em.createNamedQuery("Representative.search");
        q.setParameter("query", "%" + query + "%");

        return q.getResultList();
    }

    public List<Representative> search(
            String nameFilter,
            String nameOperator,
            String telephoneFilter,
            String telephoneOperator,
            String faxFilter,
            String faxOperator,
            String emailFilter,
            String emailOperator,
            String functionFilter,
            String functionOperator,
            String[] institutionFilter,
            String[] iTypeFilter,
            String[] iRegionFilter,
            String[] iFamilyFilter,
            String[] iCityFilter,
            String orderBy,
            int firstResult,
            int maxResults,
            boolean isQuickSearch) {

        
        
        RepresentativeSearchQueryBuilder rsqb = new RepresentativeSearchQueryBuilder(
                nameFilter,
                nameOperator,
                telephoneFilter,
                telephoneOperator,
                faxFilter,
                faxOperator,
                emailFilter,
                emailOperator,
                functionFilter,
                functionOperator,
                institutionFilter,
                iTypeFilter,
                iRegionFilter,
                iFamilyFilter,
                iCityFilter,
                false,
                orderBy,
                isQuickSearch);

        String queryS = rsqb.getQuery();

        Query query = em.createQuery(queryS);

        if (maxResults != -1 && firstResult != -1) {
            query.setFirstResult(firstResult);
            query.setMaxResults(maxResults);
        }


        List<Representative> result = null;

        result = query.getResultList();

        return result;

    }

    public Long getSearchResultCount(
            String nameFilter,
            String nameOperator,
            String telephoneFilter,
            String telephoneOperator,
            String faxFilter,
            String faxOperator,
            String emailFilter,
            String emailOperator,
            String functionFilter,
            String functionOperator,
            String[] institutionFilter,
            String[] iTypeFilter,
            String[] iRegionFilter,
            String[] iFamilyFilter,
            String[] iCityFilter,
            String orderBy) {

            

        RepresentativeSearchQueryBuilder rsqb = new RepresentativeSearchQueryBuilder(
                nameFilter,
                nameOperator,
                telephoneFilter,
                telephoneOperator,
                faxFilter,
                faxOperator,
                emailFilter,
                emailOperator,
                functionFilter,
                functionOperator,
                institutionFilter,
                iTypeFilter,
                iRegionFilter,
                iFamilyFilter,
                iCityFilter,
                true,
                orderBy,
                false);

        String queryS = rsqb.getQuery();
        Query query = em.createQuery(queryS);

        return (Long) query.getSingleResult();



    }

    public List<Representative> findSimilar(String firstName,
            String lastName,
            Institution i){

        Query q = em.createNamedQuery("Representative.findSimilar");
        q.setParameter("firstName", "%" + firstName + "%");
        q.setParameter("lastName", "%" + lastName + "%");
        q.setParameter("institution", i);

        return (List<Representative>) q.getResultList();
    }
}
